<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>条件欧式聚类分割 | 荒岛</title><meta name="author" content="Fi9zero"><meta name="copyright" content="Fi9zero"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="PCL 条件欧几里得聚类🧩 1. 程序功能概述基于 条件欧几里得聚类（Conditional Euclidean Clustering） 的点云分割系统。它不仅利用空间距离进行聚类，还融合了强度（intensity） 和 法线（normal） 等属性信息，通过自定义“相似性条件”来判断两个点是否应属于同一聚类。  💡 适用于激光雷达点云中建筑物、地面、植被等具有几何或反射特性差异的目标分割。">
<meta property="og:type" content="article">
<meta property="og:title" content="条件欧式聚类分割">
<meta property="og:url" content="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/index.html">
<meta property="og:site_name" content="荒岛">
<meta property="og:description" content="PCL 条件欧几里得聚类🧩 1. 程序功能概述基于 条件欧几里得聚类（Conditional Euclidean Clustering） 的点云分割系统。它不仅利用空间距离进行聚类，还融合了强度（intensity） 和 法线（normal） 等属性信息，通过自定义“相似性条件”来判断两个点是否应属于同一聚类。  💡 适用于激光雷达点云中建筑物、地面、植被等具有几何或反射特性差异的目标分割。">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://ckyfi9zero.github.io/image/avatar.jpg">
<meta property="article:published_time" content="2025-08-05T07:56:39.000Z">
<meta property="article:modified_time" content="2025-08-05T08:10:17.373Z">
<meta property="article:author" content="Fi9zero">
<meta property="article:tag" content="c++">
<meta property="article:tag" content="PCL">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://ckyfi9zero.github.io/image/avatar.jpg"><script type="application/ld+json">{
  "@context": "https://schema.org",
  "@type": "BlogPosting",
  "headline": "条件欧式聚类分割",
  "url": "https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/",
  "image": "https://ckyfi9zero.github.io/image/avatar.jpg",
  "datePublished": "2025-08-05T07:56:39.000Z",
  "dateModified": "2025-08-05T08:10:17.373Z",
  "author": [
    {
      "@type": "Person",
      "name": "Fi9zero",
      "url": "https://ckyfi9zero.github.io"
    }
  ]
}</script><link rel="shortcut icon" href="/image/favicon.ico"><link rel="canonical" href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css"><script>
    (() => {
      
    const saveToLocal = {
      set: (key, value, ttl) => {
        if (!ttl) return
        const expiry = Date.now() + ttl * 86400000
        localStorage.setItem(key, JSON.stringify({ value, expiry }))
      },
      get: key => {
        const itemStr = localStorage.getItem(key)
        if (!itemStr) return undefined
        const { value, expiry } = JSON.parse(itemStr)
        if (Date.now() > expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return value
      }
    }

    window.btf = {
      saveToLocal,
      getScript: (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        Object.entries(attr).forEach(([key, val]) => script.setAttribute(key, val))
        script.onload = script.onreadystatechange = () => {
          if (!script.readyState || /loaded|complete/.test(script.readyState)) resolve()
        }
        script.onerror = reject
        document.head.appendChild(script)
      }),
      getCSS: (url, id) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onload = link.onreadystatechange = () => {
          if (!link.readyState || /loaded|complete/.test(link.readyState)) resolve()
        }
        link.onerror = reject
        document.head.appendChild(link)
      }),
      addGlobalFn: (key, fn, name = false, parent = window) => {
        if (!false && key.startsWith('pjax')) return
        const globalFn = parent.globalFn || {}
        globalFn[key] = globalFn[key] || {}
        globalFn[key][name || Object.keys(globalFn[key]).length] = fn
        parent.globalFn = globalFn
      }
    }
  
      
      const activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      const activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }

      btf.activateDarkMode = activateDarkMode
      btf.activateLightMode = activateLightMode

      const theme = saveToLocal.get('theme')
    
          theme === 'dark' ? activateDarkMode() : theme === 'light' ? activateLightMode() : null
        
      
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        document.documentElement.classList.toggle('hide-aside', asideStatus === 'hide')
      }
    
      
    const detectApple = () => {
      if (/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)) {
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
  
    })()
  </script><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":true,"top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"未找到符合您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false,"highlightFullpage":false,"highlightMacStyle":true},
  copy: {
    success: '复制成功',
    error: '复制失败',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'null',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyloadPlugin: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '条件欧式聚类分割',
  isHighlightShrink: false,
  isToc: true,
  pageType: 'post'
}</script><link rel="stylesheet" href="/css/custom.css"><link rel="stylesheet" href="/css/pig.css"><link rel="stylesheet" href="https://npm.elemecdn.com/ethan4116-blog/lib/css/plane_v2.css"><meta name="generator" content="Hexo 7.3.0"></head><body><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img text-center"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data text-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url(/image/per%20page.jpg);"><nav id="nav"><span id="blog-info"><a class="nav-site-title" href="/"><span class="site-name">荒岛</span></a><a class="nav-page-title" href="/"><span class="site-name">条件欧式聚类分割</span><span class="site-name"><i class="fa-solid fa-circle-arrow-left"></i><span>  返回首页</span></span></a></span><div id="menus"><div id="search-button"><span class="site-page social-icon search"><i class="fas fa-search fa-fw"></i><span> 搜索</span></span></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/booklist/"><i class="fa-fw fas fa-book"></i><span> 书单</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 链接</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><span class="site-page"><i class="fas fa-bars fa-fw"></i></span></div></div></nav><div id="post-info"><h1 class="post-title">条件欧式聚类分割</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2025-08-05T07:56:39.000Z" title="发表于 2025-08-05 15:56:39">2025-08-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2025-08-05T08:10:17.373Z" title="更新于 2025-08-05 16:10:17">2025-08-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/PCL/">PCL</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title=""><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">浏览量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="container post-content" id="article-container"><h1 id="PCL-条件欧几里得聚类"><a href="#PCL-条件欧几里得聚类" class="headerlink" title="PCL 条件欧几里得聚类"></a>PCL 条件欧几里得聚类</h1><h2 id="🧩-1-程序功能概述"><a href="#🧩-1-程序功能概述" class="headerlink" title="🧩 1. 程序功能概述"></a>🧩 1. 程序功能概述</h2><p>基于 <strong>条件欧几里得聚类（Conditional Euclidean Clustering）</strong> 的点云分割系统。它不仅利用空间距离进行聚类，还融合了<strong>强度（intensity）</strong> 和 <strong>法线（normal）</strong> 等属性信息，通过自定义“相似性条件”来判断两个点是否应属于同一聚类。</p>
<blockquote>
<p>💡 适用于激光雷达点云中建筑物、地面、植被等具有几何或反射特性差异的目标分割。</p>
</blockquote>
<hr>
<h2 id="🔗-2-核心处理流程"><a href="#🔗-2-核心处理流程" class="headerlink" title="🔗 2. 核心处理流程"></a>🔗 2. 核心处理流程</h2><table>
<thead>
<tr>
<th>步骤</th>
<th>模块</th>
<th>功能说明</th>
</tr>
</thead>
<tbody><tr>
<td>1️⃣ 加载点云</td>
<td><code>pcl::io::loadPCDFile</code></td>
<td>读取 <code>.pcd</code> 格式文件</td>
</tr>
<tr>
<td>2️⃣ 下采样</td>
<td><code>VoxelGrid</code></td>
<td>减少点数，提升后续处理效率</td>
</tr>
<tr>
<td>3️⃣ 法线估计</td>
<td><code>NormalEstimation</code></td>
<td>计算每个点的法向量，用于判断表面曲率变化</td>
</tr>
<tr>
<td>4️⃣ 聚类分割</td>
<td><code>ConditionalEuclideanClustering</code></td>
<td>基于用户定义的“条件函数”进行区域生长式聚类</td>
</tr>
<tr>
<td>5️⃣ 后处理</td>
<td>聚类筛选</td>
<td>过滤太小或太大的聚类，并用 intensity 编码标签</td>
</tr>
<tr>
<td>6️⃣ 输出</td>
<td>可视化 &#x2F; 保存</td>
<td>使用双视口对比原图与结果，或保存为 PCD 文件</td>
</tr>
</tbody></table>
<hr>
<h2 id="⚙️-3-支持的三种聚类策略（通过-m-参数选择）"><a href="#⚙️-3-支持的三种聚类策略（通过-m-参数选择）" class="headerlink" title="⚙️ 3. 支持的三种聚类策略（通过 -m 参数选择）"></a>⚙️ 3. 支持的三种聚类策略（通过 <code>-m</code> 参数选择）</h2><table>
<thead>
<tr>
<th>方法</th>
<th>条件函数</th>
<th>判断逻辑</th>
</tr>
</thead>
<tbody><tr>
<td><code>-m 1</code></td>
<td><code>enforceIntensitySimilarity</code></td>
<td>仅当两点强度差 &lt; 5 时允许合并</td>
</tr>
<tr>
<td><code>-m 2</code></td>
<td><code>enforceCurvatureOrIntensitySimilarity</code></td>
<td>若强度相近 <strong>或</strong> 法线差异大（边界），则允许断开（即不强制合并）</td>
</tr>
<tr>
<td><code>-m 3</code></td>
<td><code>customRegionGrowing</code></td>
<td>距离近时宽松，距离远时严格；结合强度与法线判断</td>
</tr>
</tbody></table>
<blockquote>
<p>✅ 所有函数返回 <code>true</code> 表示：两点<strong>可以归为同一聚类</strong></p>
</blockquote>
<hr>
<h2 id="📏-4-关键参数说明（命令行控制）"><a href="#📏-4-关键参数说明（命令行控制）" class="headerlink" title="📏 4. 关键参数说明（命令行控制）"></a>📏 4. 关键参数说明（命令行控制）</h2><table>
<thead>
<tr>
<th>参数</th>
<th>示例值</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td><code>-l</code></td>
<td><code>-l 40</code></td>
<td>体素网格大小（LeafSize），单位：米，控制下采样粒度</td>
</tr>
<tr>
<td><code>-r</code></td>
<td><code>-r 300</code></td>
<td>法线估计的搜索半径（RadiusSearch），影响邻域范围</td>
</tr>
<tr>
<td><code>-v</code></td>
<td><code>-v 1</code></td>
<td>是否启用可视化（1&#x3D;是，0&#x3D;否）</td>
</tr>
<tr>
<td><code>-m</code></td>
<td><code>-m 2</code></td>
<td>选择使用的聚类条件函数类型</td>
</tr>
</tbody></table>
<p>📌 示例运行命令：</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">./cluster input.pcd -l 20 -r 150 -v 1 -m 2</span><br></pre></td></tr></table></figure>

<hr>
<h2 id="🎨-5-聚类结果编码方式（利用-intensity-字段）"><a href="#🎨-5-聚类结果编码方式（利用-intensity-字段）" class="headerlink" title="🎨 5. 聚类结果编码方式（利用 intensity 字段）"></a>🎨 5. 聚类结果编码方式（利用 intensity 字段）</h2><p>为了在不新增字段的情况下存储聚类标签，程序巧妙地复用了 <code>intensity</code> 字段：</p>
<table>
<thead>
<tr>
<th>类型</th>
<th>intensity 值</th>
<th>含义</th>
</tr>
</thead>
<tbody><tr>
<td>有效聚类</td>
<td>0~7（随机整数）</td>
<td>不同聚类赋予不同颜色</td>
</tr>
<tr>
<td>被剔除的小聚类</td>
<td>-2.0</td>
<td>可能是噪声或碎片</td>
</tr>
<tr>
<td>被剔除的大聚类</td>
<td>+10.0</td>
<td>可能是地面或主结构体</td>
</tr>
</tbody></table>
<blockquote>
<p>✅ 可视化时通过 <code>PointCloudColorHandlerGenericField</code> 按 intensity 自动着色</p>
</blockquote>
<hr>
<h2 id="👁️-6-可视化设计（双视口对比）"><a href="#👁️-6-可视化设计（双视口对比）" class="headerlink" title="👁️ 6. 可视化设计（双视口对比）"></a>👁️ 6. 可视化设计（双视口对比）</h2><p>使用 <code>PCLVisualizer</code> 创建两个并排视口：</p>
<ul>
<li><strong>左视口（v1）</strong>：显示原始点云（灰色小点）</li>
<li><strong>右视口（v2）</strong>：显示分割结果（按 intensity 彩色渲染）</li>
</ul>
<p>支持鼠标交互（旋转、缩放、平移），便于分析分割效果。</p>
<hr>
<h2 id="⚡-7-性能优化技巧"><a href="#⚡-7-性能优化技巧" class="headerlink" title="⚡ 7. 性能优化技巧"></a>⚡ 7. 性能优化技巧</h2><table>
<thead>
<tr>
<th>技术</th>
<th>作用</th>
</tr>
</thead>
<tbody><tr>
<td>体素下采样</td>
<td>显著减少点数，加快法线估计和聚类速度</td>
</tr>
<tr>
<td>KdTree 加速搜索</td>
<td>提高邻域查找效率（O(log n)）</td>
</tr>
<tr>
<td>法线 + 强度融合</td>
<td>提升聚类语义准确性，避免纯距离导致的过分割</td>
</tr>
<tr>
<td>聚类尺寸过滤</td>
<td>避免极小（噪声）或极大（背景）聚类干扰结果</td>
</tr>
</tbody></table>
<hr>
<h2 id="⚠️-8-注意事项"><a href="#⚠️-8-注意事项" class="headerlink" title="⚠️ 8. 注意事项"></a>⚠️ 8. 注意事项</h2><table>
<thead>
<tr>
<th>问题</th>
<th>解决建议</th>
</tr>
</thead>
<tbody><tr>
<td>法线估计太慢？</td>
<td>减小 <code>-r</code> 半径，或先大幅下采样</td>
</tr>
<tr>
<td>聚类断裂？</td>
<td>调整 <code>setClusterTolerance</code> 或条件函数阈值</td>
</tr>
<tr>
<td>颜色显示异常？</td>
<td>确保 <code>color_handler</code> 正确绑定 <code>&quot;intensity&quot;</code> 字段</td>
</tr>
<tr>
<td>编译报错 <code>Eigen::Map</code>？</td>
<td>确保包含头文件且数组连续存储</td>
</tr>
</tbody></table>
<hr>
<h2 id="➕-9-扩展建议（进阶方向）"><a href="#➕-9-扩展建议（进阶方向）" class="headerlink" title="➕ 9. 扩展建议（进阶方向）"></a>➕ 9. 扩展建议（进阶方向）</h2><ul>
<li>支持 RGB 点云（<code>PointXYZRGB</code>）并按颜色聚类</li>
<li>输出聚类统计信息（数量、平均点数、质心等）</li>
<li>支持更多输入格式（<code>.ply</code>, <code>.bin</code>, <code>.las</code>）</li>
<li>将每个聚类保存为独立 <code>.pcd</code> 文件</li>
<li>结合 RANSAC 地面去除作为预处理步骤</li>
</ul>
<hr>
<h2 id="代码实现"><a href="#代码实现" class="headerlink" title="代码实现"></a>代码实现</h2><figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br><span class="line">146</span><br><span class="line">147</span><br><span class="line">148</span><br><span class="line">149</span><br><span class="line">150</span><br><span class="line">151</span><br><span class="line">152</span><br><span class="line">153</span><br><span class="line">154</span><br><span class="line">155</span><br><span class="line">156</span><br><span class="line">157</span><br><span class="line">158</span><br><span class="line">159</span><br><span class="line">160</span><br><span class="line">161</span><br><span class="line">162</span><br><span class="line">163</span><br><span class="line">164</span><br><span class="line">165</span><br><span class="line">166</span><br><span class="line">167</span><br><span class="line">168</span><br><span class="line">169</span><br><span class="line">170</span><br><span class="line">171</span><br><span class="line">172</span><br><span class="line">173</span><br><span class="line">174</span><br><span class="line">175</span><br><span class="line">176</span><br><span class="line">177</span><br><span class="line">178</span><br><span class="line">179</span><br><span class="line">180</span><br><span class="line">181</span><br><span class="line">182</span><br><span class="line">183</span><br><span class="line">184</span><br><span class="line">185</span><br><span class="line">186</span><br><span class="line">187</span><br><span class="line">188</span><br><span class="line">189</span><br><span class="line">190</span><br><span class="line">191</span><br><span class="line">192</span><br><span class="line">193</span><br><span class="line">194</span><br><span class="line">195</span><br><span class="line">196</span><br><span class="line">197</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/point_types.h&gt;</span>                         <span class="comment">// PCL 内置点类型定义（如PointXYZ, PointXYZI等）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/io/pcd_io.h&gt;</span>                           <span class="comment">// 提供 PCD 文件的读写功能（loadPCDFile, savePCDFile）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/console/time.h&gt;</span>                        <span class="comment">// 提供 TicToc 类用于时间性能统计</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;iostream&gt;</span>                                  <span class="comment">// 标准输入输出流（std::cout, std::cerr）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;ostream&gt;</span>                                   <span class="comment">// 输出流基础类（支持 &lt;&lt; 操作符）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/filters/voxel_grid.h&gt;</span>                  <span class="comment">// 体素网格滤波器，用于点云下采样</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/features/normal_3d.h&gt;</span>                  <span class="comment">// 法线估计模块，计算每个点的法向量</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/segmentation/conditional_euclidean_clustering.h&gt;</span> <span class="comment">// 条件欧几里得聚类算法</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/console/parse.h&gt;</span>                       <span class="comment">// 命令行参数解析工具（parse_argument）</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/visualization/pcl_visualizer.h&gt;</span>        <span class="comment">// 可视化类，支持3D点云渲染和交互</span></span></span><br><span class="line"><span class="meta">#<span class="keyword">include</span> <span class="string">&lt;pcl/visualization/point_cloud_color_handlers.h&gt;</span> <span class="comment">// 点云颜色处理器，按字段着色</span></span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 定义常用类型别名</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointXYZI PointTypeIO;                   <span class="comment">// 输入/输出点类型：XYZ坐标 + 强度(intensity)</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::PointXYZINormal PointTypeFull;           <span class="comment">// 包含法线信息的完整点类型</span></span><br><span class="line"><span class="keyword">typedef</span> pcl::visualization::PointCloudColorHandler&lt;pcl::PCLPointCloud2&gt; ColorHandler; <span class="comment">// 通用颜色处理器基类</span></span><br><span class="line"><span class="keyword">typedef</span> ColorHandler::Ptr ColorHandlerPtr;            <span class="comment">// 智能指针</span></span><br><span class="line"><span class="keyword">typedef</span> ColorHandler::ConstPtr ColorHandlerConstPtr;  <span class="comment">// 常量智能指针</span></span><br><span class="line"><span class="keyword">using</span> <span class="keyword">namespace</span> pcl::console;                         <span class="comment">// 使用PCL控制台命名空间，简化函数调用</span></span><br><span class="line"></span><br><span class="line"><span class="comment">// 条件函数1：仅基于强度相似性</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">enforceIntensitySimilarity</span> <span class="params">(<span class="type">const</span> PointTypeFull&amp; point_a, <span class="type">const</span> PointTypeFull&amp; point_b, <span class="type">float</span> squared_distance)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">fabs</span> ((<span class="type">float</span>)point_a.intensity - (<span class="type">float</span>)point_b.intensity) &lt; <span class="number">5.0f</span>) <span class="comment">// 强度差小于5则认为相似</span></span><br><span class="line">    <span class="keyword">return</span> (<span class="literal">true</span>);</span><br><span class="line">  <span class="keyword">else</span></span><br><span class="line">    <span class="built_in">return</span> (<span class="literal">false</span>); <span class="comment">// 否则不满足条件</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 条件函数2：基于强度或法线相似性（任一满足即可合并）</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">enforceCurvatureOrIntensitySimilarity</span> <span class="params">(<span class="type">const</span> PointTypeFull&amp; point_a, <span class="type">const</span> PointTypeFull&amp; point_b, <span class="type">float</span> squared_distance)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="comment">// 使用 Eigen::Map 将 normal[3] 数组映射为 Vector3f 向量以便进行点积运算</span></span><br><span class="line">  Eigen::Map&lt;<span class="type">const</span> Eigen::Vector3f&gt; point_a_normal = Eigen::<span class="built_in">Map</span>&lt;<span class="type">const</span> Eigen::Vector3f&gt;(point_a.normal);</span><br><span class="line">  Eigen::Map&lt;<span class="type">const</span> Eigen::Vector3f&gt; point_b_normal = Eigen::<span class="built_in">Map</span>&lt;<span class="type">const</span> Eigen::Vector3f&gt;(point_b.normal);</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">fabs</span> ((<span class="type">float</span>)point_a.intensity - (<span class="type">float</span>)point_b.intensity) &lt; <span class="number">5.0f</span>) <span class="comment">// 强度相近 → 允许聚类</span></span><br><span class="line">    <span class="keyword">return</span> (<span class="literal">true</span>);</span><br><span class="line">  <span class="keyword">if</span> (<span class="built_in">fabs</span> (point_a_normal.<span class="built_in">dot</span> (point_b_normal)) &lt; <span class="number">0.05</span>) <span class="comment">// 法线夹角大（点积小）→ 边界区域 → 允许断开（即允许不聚类）</span></span><br><span class="line">    <span class="keyword">return</span> (<span class="literal">true</span>);</span><br><span class="line">  <span class="keyword">return</span> (<span class="literal">false</span>); <span class="comment">// 都不满足 → 不合并两点</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 条件函数3：自定义区域生长策略（距离相关）</span></span><br><span class="line"><span class="function"><span class="type">bool</span> <span class="title">customRegionGrowing</span> <span class="params">(<span class="type">const</span> PointTypeFull&amp; point_a, <span class="type">const</span> PointTypeFull&amp; point_b, <span class="type">float</span> squared_distance)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  Eigen::Map&lt;<span class="type">const</span> Eigen::Vector3f&gt; point_a_normal = Eigen::<span class="built_in">Map</span>&lt;<span class="type">const</span> Eigen::Vector3f&gt;(point_a.normal);</span><br><span class="line">  Eigen::Map&lt;<span class="type">const</span> Eigen::Vector3f&gt; point_b_normal = Eigen::<span class="built_in">Map</span>&lt;<span class="type">const</span> Eigen::Vector3f&gt;(point_b.normal);</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">if</span> (squared_distance &lt; <span class="number">10000</span>) <span class="comment">// 若两点距离较近（&lt;100单位）</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">fabs</span> ((<span class="type">float</span>)point_a.intensity - (<span class="type">float</span>)point_b.intensity) &lt; <span class="number">8.0f</span>) <span class="comment">// 宽松强度阈值</span></span><br><span class="line">      <span class="keyword">return</span> (<span class="literal">true</span>);</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">fabs</span> (point_a_normal.<span class="built_in">dot</span> (point_b_normal)) &lt; <span class="number">0.06</span>) <span class="comment">// 法线差异大 → 可接受为边界</span></span><br><span class="line">      <span class="keyword">return</span> (<span class="literal">true</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> <span class="comment">// 距离远时要求更严格</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">if</span> (<span class="built_in">fabs</span> ((<span class="type">float</span>)point_a.intensity - (<span class="type">float</span>)point_b.intensity) &lt; <span class="number">3.0f</span>) <span class="comment">// 更小的强度差异才允许聚类</span></span><br><span class="line">      <span class="built_in">return</span> (<span class="literal">true</span>);</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">return</span> (<span class="literal">false</span>); <span class="comment">// 不满足任何条件 → 不聚类</span></span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="type">int</span> <span class="title">main</span> <span class="params">(<span class="type">int</span> argc, <span class="type">char</span>** argv)</span></span></span><br><span class="line"><span class="function"></span>&#123;</span><br><span class="line">  <span class="comment">// 检查输入参数数量</span></span><br><span class="line">  <span class="keyword">if</span>(argc &lt; <span class="number">2</span>)</span><br><span class="line">  &#123;</span><br><span class="line">    std::cout &lt;&lt; <span class="string">&quot;.exe xx.pcd -l 40 -r 300.0 -v 1 -m 1/2/3&quot;</span> &lt;&lt; std::endl; <span class="comment">// 提示用法</span></span><br><span class="line">    <span class="keyword">return</span> <span class="number">0</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="type">bool</span> Visual = <span class="literal">true</span>; <span class="comment">// 是否启用可视化（默认开启）</span></span><br><span class="line">  <span class="type">float</span> Leaf = <span class="number">40</span>, Radius = <span class="number">300</span>; <span class="comment">// 体素大小（Leaf）和法线估计搜索半径（Radius）</span></span><br><span class="line">  <span class="type">int</span> Method = <span class="number">1</span>; <span class="comment">// 聚类方法选择：1=强度；2=强度或法线；3=自定义</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 解析命令行参数</span></span><br><span class="line">  <span class="built_in">parse_argument</span> (argc, argv, <span class="string">&quot;-l&quot;</span>, Leaf);           <span class="comment">// -l 设置体素尺寸</span></span><br><span class="line">  <span class="built_in">parse_argument</span> (argc, argv, <span class="string">&quot;-r&quot;</span>, Radius);         <span class="comment">// -r 设置法线搜索半径</span></span><br><span class="line">  <span class="built_in">parse_argument</span> (argc, argv, <span class="string">&quot;-v&quot;</span>, Visual);         <span class="comment">// -v 控制是否可视化</span></span><br><span class="line">  <span class="built_in">parse_argument</span> (argc, argv, <span class="string">&quot;-m&quot;</span>, Method);         <span class="comment">// -m 选择聚类条件函数</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 数据容器声明</span></span><br><span class="line">  pcl::PointCloud&lt;PointTypeIO&gt;::<span class="function">Ptr <span class="title">cloud_in</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointTypeIO&gt;)</span></span>; <span class="comment">// 原始输入点云</span></span><br><span class="line">  pcl::PointCloud&lt;PointTypeIO&gt;::<span class="function">Ptr <span class="title">cloud_out</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointTypeIO&gt;)</span></span>; <span class="comment">// 下采样后点云</span></span><br><span class="line">  pcl::PointCloud&lt;PointTypeFull&gt;::<span class="function">Ptr <span class="title">cloud_with_normals</span> <span class="params">(<span class="keyword">new</span> pcl::PointCloud&lt;PointTypeFull&gt;)</span></span>; <span class="comment">// 带法线的点云</span></span><br><span class="line">  <span class="function">pcl::IndicesClustersPtr <span class="title">clusters</span> <span class="params">(<span class="keyword">new</span> pcl::IndicesClusters)</span></span>; <span class="comment">// 存储有效聚类（索引组）</span></span><br><span class="line">  <span class="function">pcl::IndicesClustersPtr <span class="title">small_clusters</span> <span class="params">(<span class="keyword">new</span> pcl::IndicesClusters)</span></span>; <span class="comment">// 被剔除的小聚类</span></span><br><span class="line">  <span class="function">pcl::IndicesClustersPtr <span class="title">large_clusters</span> <span class="params">(<span class="keyword">new</span> pcl::IndicesClusters)</span></span>; <span class="comment">// 被剔除的大聚类</span></span><br><span class="line">  pcl::search::KdTree&lt;PointTypeIO&gt;::<span class="function">Ptr <span class="title">search_tree</span> <span class="params">(<span class="keyword">new</span> pcl::search::KdTree&lt;PointTypeIO&gt;)</span></span>; <span class="comment">// KdTree用于加速搜索</span></span><br><span class="line">  pcl::console::TicToc tt; <span class="comment">// 计时器对象</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 加载输入点云</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;Loading...\n&quot;</span>, tt.<span class="built_in">tic</span> (); <span class="comment">// 输出提示 + 开始计时</span></span><br><span class="line">  pcl::io::<span class="built_in">loadPCDFile</span> (argv[<span class="number">1</span>], *cloud_in); <span class="comment">// 从第一个命令行参数加载PCD文件</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;&gt;&gt; Done: &quot;</span> &lt;&lt; tt.<span class="built_in">toc</span> () &lt;&lt; <span class="string">&quot; ms, &quot;</span> &lt;&lt; cloud_in-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; points\n&quot;</span>; <span class="comment">// 打印耗时和点数</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 体素网格下采样</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;Downsampling...\n&quot;</span>, tt.<span class="built_in">tic</span> ();</span><br><span class="line">  pcl::VoxelGrid&lt;PointTypeIO&gt; vg; <span class="comment">// 创建体素滤波器对象</span></span><br><span class="line">  vg.<span class="built_in">setInputCloud</span> (cloud_in); <span class="comment">// 设置输入</span></span><br><span class="line">  vg.<span class="built_in">setLeafSize</span> (Leaf, Leaf, Leaf); <span class="comment">// 设置体素边长</span></span><br><span class="line">  vg.<span class="built_in">setDownsampleAllData</span> (<span class="literal">true</span>); <span class="comment">// 对所有字段（包括intensity）进行下采样</span></span><br><span class="line">  vg.<span class="built_in">filter</span> (*cloud_out); <span class="comment">// 执行滤波，结果存入cloud_out</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;&gt;&gt; Done: &quot;</span> &lt;&lt; tt.<span class="built_in">toc</span> () &lt;&lt; <span class="string">&quot; ms, &quot;</span> &lt;&lt; cloud_out-&gt;points.<span class="built_in">size</span> () &lt;&lt; <span class="string">&quot; points\n&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 计算法线</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;Computing normals...\n&quot;</span>, tt.<span class="built_in">tic</span> ();</span><br><span class="line">  pcl::<span class="built_in">copyPointCloud</span> (*cloud_out, *cloud_with_normals); <span class="comment">// 复制点和强度到带法线的点云中</span></span><br><span class="line">  pcl::NormalEstimation&lt;PointTypeIO, PointTypeFull&gt; ne; <span class="comment">// 创建法线估计对象</span></span><br><span class="line">  ne.<span class="built_in">setInputCloud</span> (cloud_out); <span class="comment">// 设置输入点云</span></span><br><span class="line">  ne.<span class="built_in">setSearchMethod</span> (search_tree); <span class="comment">// 设置搜索结构（KdTree加速）</span></span><br><span class="line">  ne.<span class="built_in">setRadiusSearch</span> (Radius); <span class="comment">// 设置以该点半径内的邻域计算法线</span></span><br><span class="line">  ne.<span class="built_in">compute</span> (*cloud_with_normals); <span class="comment">// 执行法线估计，结果填充到cloud_with_normals</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;&gt;&gt; Done: &quot;</span> &lt;&lt; tt.<span class="built_in">toc</span> () &lt;&lt; <span class="string">&quot; ms\n&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 设置条件欧几里得聚类</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;Segmenting to clusters...\n&quot;</span>, tt.<span class="built_in">tic</span> ();</span><br><span class="line">  <span class="function">pcl::ConditionalEuclideanClustering&lt;PointTypeFull&gt; <span class="title">cec</span> <span class="params">(<span class="literal">true</span>)</span></span>; <span class="comment">// 初始化聚类对象，允许自定义条件</span></span><br><span class="line">  cec.<span class="built_in">setInputCloud</span> (cloud_with_normals); <span class="comment">// 设置输入点云（带法线）</span></span><br><span class="line"></span><br><span class="line">  <span class="comment">// 根据用户选择设置不同的聚类条件函数</span></span><br><span class="line">  <span class="keyword">switch</span>(Method)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">1</span>:</span><br><span class="line">      cec.<span class="built_in">setConditionFunction</span> (&amp;enforceIntensitySimilarity); <span class="comment">// 仅强度相似</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">2</span>:</span><br><span class="line">      cec.<span class="built_in">setConditionFunction</span> (&amp;enforceCurvatureOrIntensitySimilarity); <span class="comment">// 强度或法线差异大</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">case</span> <span class="number">3</span>:</span><br><span class="line">      cec.<span class="built_in">setConditionFunction</span> (&amp;customRegionGrowing); <span class="comment">// 自定义距离相关规则</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">    <span class="keyword">default</span>:</span><br><span class="line">      cec.<span class="built_in">setConditionFunction</span> (&amp;customRegionGrowing); <span class="comment">// 默认使用方法3</span></span><br><span class="line">      <span class="keyword">break</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  cec.<span class="built_in">setClusterTolerance</span> (<span class="number">500.0</span>); <span class="comment">// 聚类时的邻域搜索半径（单位：距离平方？实际是欧氏距离）</span></span><br><span class="line">  cec.<span class="built_in">setMinClusterSize</span> (cloud_with_normals-&gt;points.<span class="built_in">size</span> () / <span class="number">1000</span>); <span class="comment">// 最小聚类点数（千分之一）</span></span><br><span class="line">  cec.<span class="built_in">setMaxClusterSize</span> (cloud_with_normals-&gt;points.<span class="built_in">size</span> () / <span class="number">5</span>); <span class="comment">// 最大聚类点数（五分之一）</span></span><br><span class="line">  cec.<span class="built_in">segment</span> (*clusters); <span class="comment">// 执行聚类，结果存入clusters</span></span><br><span class="line">  cec.<span class="built_in">getRemovedClusters</span> (small_clusters, large_clusters); <span class="comment">// 获取因太小/太大被剔除的聚类</span></span><br><span class="line">  std::cerr &lt;&lt; <span class="string">&quot;&gt;&gt; Done: &quot;</span> &lt;&lt; tt.<span class="built_in">toc</span> () &lt;&lt; <span class="string">&quot; ms\n&quot;</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 标记被剔除的聚类：小聚类标记为 -2.0</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; small_clusters-&gt;<span class="built_in">size</span> (); ++i)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; (*small_clusters)[i].indices.<span class="built_in">size</span> (); ++j)</span><br><span class="line">      cloud_out-&gt;points[(*small_clusters)[i].indices[j]].intensity = <span class="number">-2.0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 大聚类标记为 +10.0</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; large_clusters-&gt;<span class="built_in">size</span> (); ++i)</span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; (*large_clusters)[i].indices.<span class="built_in">size</span> (); ++j)</span><br><span class="line">      cloud_out-&gt;points[(*large_clusters)[i].indices[j]].intensity = <span class="number">+10.0</span>;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 有效聚类随机着色（0~7）</span></span><br><span class="line">  <span class="keyword">for</span> (<span class="type">int</span> i = <span class="number">0</span>; i &lt; clusters-&gt;<span class="built_in">size</span> (); ++i)</span><br><span class="line">  &#123;</span><br><span class="line">    <span class="type">int</span> label = <span class="built_in">rand</span> () % <span class="number">8</span>; <span class="comment">// 生成0-7的随机标签</span></span><br><span class="line">    <span class="keyword">for</span> (<span class="type">int</span> j = <span class="number">0</span>; j &lt; (*clusters)[i].indices.<span class="built_in">size</span> (); ++j)</span><br><span class="line">      cloud_out-&gt;points[(*clusters)[i].indices[j]].intensity = label; <span class="comment">// 赋值给intensity字段用于可视化</span></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 可视化或保存结果</span></span><br><span class="line">  <span class="keyword">if</span>(Visual) <span class="comment">// 如果启用可视化</span></span><br><span class="line">  &#123;</span><br><span class="line">    <span class="comment">// 创建双视口可视化器</span></span><br><span class="line">    <span class="function">boost::shared_ptr&lt;pcl::visualization::PCLVisualizer&gt; <span class="title">MView</span> <span class="params">(<span class="keyword">new</span> pcl::visualization::PCLVisualizer (<span class="string">&quot;Conditional Euclidean Clustering&quot;</span>))</span></span>;</span><br><span class="line">    <span class="function"><span class="type">int</span> <span class="title">v1</span><span class="params">(<span class="number">0</span>)</span>, <span class="title">v2</span><span class="params">(<span class="number">0</span>)</span></span>;</span><br><span class="line">    MView-&gt;<span class="built_in">createViewPort</span> (<span class="number">0.0</span>, <span class="number">0.0</span>, <span class="number">0.5</span>, <span class="number">1.0</span>, v1); <span class="comment">// 左半屏</span></span><br><span class="line">    MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">1</span>, <span class="number">0.2</span>, <span class="number">1</span>, v1); <span class="comment">// 紫色背景</span></span><br><span class="line">    MView-&gt;<span class="built_in">addText</span> (<span class="string">&quot;Original Cloud&quot;</span>, <span class="number">10</span>, <span class="number">10</span>, <span class="string">&quot;Before segmentation&quot;</span>, v1); <span class="comment">// 添加文字说明</span></span><br><span class="line">    MView-&gt;<span class="built_in">createViewPort</span> (<span class="number">0.5</span>, <span class="number">0.0</span>, <span class="number">1.0</span>, <span class="number">1.0</span>, v2); <span class="comment">// 右半屏</span></span><br><span class="line">    MView-&gt;<span class="built_in">setBackgroundColor</span> (<span class="number">0.5</span>, <span class="number">0.5</span>, <span class="number">0.5</span>, v2); <span class="comment">// 灰色背景</span></span><br><span class="line">    MView-&gt;<span class="built_in">addText</span> (<span class="string">&quot;Segmented Cloud&quot;</span>, <span class="number">10</span>, <span class="number">10</span>, <span class="string">&quot;After segmentation&quot;</span>, v2);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 显示原始点云（左视口）</span></span><br><span class="line">    MView-&gt;<span class="built_in">addPointCloud</span>&lt;pcl::PointXYZI&gt;(cloud_in, <span class="string">&quot;input&quot;</span>, v1);</span><br><span class="line">    MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, <span class="number">1</span>, <span class="string">&quot;input&quot;</span>, v1);</span><br><span class="line"></span><br><span class="line">    <span class="comment">// 显示分割后点云（右视口），根据intensity字段自动着色</span></span><br><span class="line">    pcl::<span class="function">visualization::PointCloudColorHandlerGenericField&lt;pcl::PointXYZI&gt; <span class="title">color_handler</span><span class="params">(cloud_out, <span class="string">&quot;intensity&quot;</span>)</span></span>;</span><br><span class="line">    MView-&gt;<span class="built_in">addPointCloud</span>&lt;pcl::PointXYZI&gt;(cloud_out, color_handler, <span class="string">&quot;output&quot;</span>, v2);</span><br><span class="line">    MView-&gt;<span class="built_in">setPointCloudRenderingProperties</span>(pcl::visualization::PCL_VISUALIZER_POINT_SIZE, <span class="number">3</span>, <span class="string">&quot;output&quot;</span>, v2);</span><br><span class="line"></span><br><span class="line">    MView-&gt;<span class="built_in">spin</span>(); <span class="comment">// 进入可视化主循环，直到窗口关闭</span></span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">else</span> <span class="comment">// 未启用可视化 → 保存结果到文件</span></span><br><span class="line">  &#123;</span><br><span class="line">    std::cerr &lt;&lt; <span class="string">&quot;Saving...\n&quot;</span>, tt.<span class="built_in">tic</span> ();</span><br><span class="line">    pcl::io::<span class="built_in">savePCDFile</span> (<span class="string">&quot;output.pcd&quot;</span>, *cloud_out); <span class="comment">// 保存为 output.pcd</span></span><br><span class="line">    std::cerr &lt;&lt; <span class="string">&quot;&gt;&gt; Done: &quot;</span> &lt;&lt; tt.<span class="built_in">toc</span> () &lt;&lt; <span class="string">&quot; ms\n&quot;</span>;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">return</span> (<span class="number">0</span>); <span class="comment">// 程序正常退出</span></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io">Fi9zero</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/">https://ckyfi9zero.github.io/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来源 <a href="https://ckyfi9zero.github.io" target="_blank">荒岛</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/c/">c++</a><a class="post-meta__tags" href="/tags/PCL/">PCL</a></div><div class="post-share"><div class="social-share" data-image="/image/avatar.jpg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E8%BF%90%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%88%86%E5%89%B2%E4%B8%8E%E9%85%8D%E5%87%86%E7%AE%97%E6%B3%95/" title="运动对象分割与配准算法"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info"><div class="info-1"><div class="info-item-1">上一篇</div><div class="info-item-2">运动对象分割与配准算法</div></div></div></a><a class="pagination-related  no-desc" href="/2025/08/05/2025-08-05-%E6%B8%90%E8%BF%9B%E5%BC%8F%E5%BD%A2%E6%80%81%E5%AD%A6%E6%BB%A4%E6%B3%A2%E5%9C%B0%E9%9D%A2%E5%88%86%E5%89%B2/" title="渐进式形态学滤波地面分割"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-right"><div class="info-1"><div class="info-item-1">下一篇</div><div class="info-item-2">渐进式形态学滤波地面分割</div></div></div></a></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span>相关推荐</span></div><div class="relatedPosts-list"><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8kdtree%E5%AE%9E%E7%8E%B0%E5%BF%AB%E9%80%9F%E9%A2%86%E5%9F%9F%E6%90%9C%E7%B4%A2/" title="用kdtree实现快速领域搜索"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用kdtree实现快速领域搜索</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E5%85%AB%E5%8F%89%E6%A0%91%E8%BF%9B%E8%A1%8C%E6%97%A0%E5%BA%8F%E7%82%B9%E4%BA%91%E6%95%B0%E6%8D%AE%E7%9A%84%E7%A9%BA%E9%97%B4%E5%8F%98%E5%8C%96%E6%A3%80%E6%B5%8B/" title="用八叉树进行无序点云数据的空间变化检测"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用八叉树进行无序点云数据的空间变化检测</div></div></div></a><a class="pagination-related no-desc" href="/2025/07/31/2025-07-31-%E7%94%A8%E7%9B%B4%E9%80%9A%E6%BB%A4%E6%B3%A2%E5%99%A8%E5%AF%B9%E7%82%B9%E4%BA%91%E8%BF%9B%E8%A1%8C%E6%BB%A4%E6%B3%A2%E5%A4%84%E7%90%86/" title="用直通滤波器对点云进行滤波处理"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-07-31</div><div class="info-item-2">用直通滤波器对点云进行滤波处理</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E7%82%B9%E4%BA%91%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%E6%B7%B1%E5%BA%A6%E5%9B%BE%E5%83%8F/" title="从点云创建一个深度图像"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从点云创建一个深度图像</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E4%BB%8E%E4%B8%80%E4%B8%AA%E7%82%B9%E4%BA%91%E4%B8%AD%E6%8F%90%E5%8F%96%E4%B8%80%E4%B8%AA%E5%AD%90%E9%9B%86/" title="从一个点云中提取一个子集"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">从一个点云中提取一个子集</div></div></div></a><a class="pagination-related no-desc" href="/2025/08/01/2025-08-01-%E5%9C%A8%E6%BB%A4%E6%B3%A2%E5%99%A8%E6%A8%A1%E5%9D%97%E7%A7%BB%E9%99%A4%E7%A6%BB%E7%BE%A4%E7%82%B9/" title="在滤波器模块移除离群点"><div class="cover" style="background: var(--default-bg-color)"></div><div class="info text-center"><div class="info-1"><div class="info-item-1"><i class="far fa-calendar-alt fa-fw"></i> 2025-08-01</div><div class="info-item-2">在滤波器模块移除离群点</div></div></div></a></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info text-center"><div class="avatar-img"><img src="/image/avatar.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info-name">Fi9zero</div><div class="author-info-description"></div><div class="site-data"><a href="/archives/"><div class="headline">文章</div><div class="length-num">54</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">15</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">8</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/ckyFi9zero"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons"><a class="social-icon" href="https://github.com/ckyFi9zero" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="mailto:2539894206@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#PCL-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%87%A0%E9%87%8C%E5%BE%97%E8%81%9A%E7%B1%BB"><span class="toc-number">1.</span> <span class="toc-text">PCL 条件欧几里得聚类</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%A7%A9-1-%E7%A8%8B%E5%BA%8F%E5%8A%9F%E8%83%BD%E6%A6%82%E8%BF%B0"><span class="toc-number">1.1.</span> <span class="toc-text">🧩 1. 程序功能概述</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%94%97-2-%E6%A0%B8%E5%BF%83%E5%A4%84%E7%90%86%E6%B5%81%E7%A8%8B"><span class="toc-number">1.2.</span> <span class="toc-text">🔗 2. 核心处理流程</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%9A%99%EF%B8%8F-3-%E6%94%AF%E6%8C%81%E7%9A%84%E4%B8%89%E7%A7%8D%E8%81%9A%E7%B1%BB%E7%AD%96%E7%95%A5%EF%BC%88%E9%80%9A%E8%BF%87-m-%E5%8F%82%E6%95%B0%E9%80%89%E6%8B%A9%EF%BC%89"><span class="toc-number">1.3.</span> <span class="toc-text">⚙️ 3. 支持的三种聚类策略（通过 -m 参数选择）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%93%8F-4-%E5%85%B3%E9%94%AE%E5%8F%82%E6%95%B0%E8%AF%B4%E6%98%8E%EF%BC%88%E5%91%BD%E4%BB%A4%E8%A1%8C%E6%8E%A7%E5%88%B6%EF%BC%89"><span class="toc-number">1.4.</span> <span class="toc-text">📏 4. 关键参数说明（命令行控制）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%8E%A8-5-%E8%81%9A%E7%B1%BB%E7%BB%93%E6%9E%9C%E7%BC%96%E7%A0%81%E6%96%B9%E5%BC%8F%EF%BC%88%E5%88%A9%E7%94%A8-intensity-%E5%AD%97%E6%AE%B5%EF%BC%89"><span class="toc-number">1.5.</span> <span class="toc-text">🎨 5. 聚类结果编码方式（利用 intensity 字段）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%F0%9F%91%81%EF%B8%8F-6-%E5%8F%AF%E8%A7%86%E5%8C%96%E8%AE%BE%E8%AE%A1%EF%BC%88%E5%8F%8C%E8%A7%86%E5%8F%A3%E5%AF%B9%E6%AF%94%EF%BC%89"><span class="toc-number">1.6.</span> <span class="toc-text">👁️ 6. 可视化设计（双视口对比）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%9A%A1-7-%E6%80%A7%E8%83%BD%E4%BC%98%E5%8C%96%E6%8A%80%E5%B7%A7"><span class="toc-number">1.7.</span> <span class="toc-text">⚡ 7. 性能优化技巧</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%9A%A0%EF%B8%8F-8-%E6%B3%A8%E6%84%8F%E4%BA%8B%E9%A1%B9"><span class="toc-number">1.8.</span> <span class="toc-text">⚠️ 8. 注意事项</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E2%9E%95-9-%E6%89%A9%E5%B1%95%E5%BB%BA%E8%AE%AE%EF%BC%88%E8%BF%9B%E9%98%B6%E6%96%B9%E5%90%91%EF%BC%89"><span class="toc-number">1.9.</span> <span class="toc-text">➕ 9. 扩展建议（进阶方向）</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BB%A3%E7%A0%81%E5%AE%9E%E7%8E%B0"><span class="toc-number">1.10.</span> <span class="toc-text">代码实现</span></a></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/10/05/2025-10-05-lio_sam/" title="lio_sam">lio_sam</a><time datetime="2025-10-05T02:43:51.000Z" title="发表于 2025-10-05 10:43:51">2025-10-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/12/2025-08-12-%E4%BD%BF%E7%94%A8docker%E6%90%AD%E5%BB%BApytorch%E7%8E%AF%E5%A2%83/" title="使用docker搭建pytorch环境">使用docker搭建pytorch环境</a><time datetime="2025-08-12T12:43:33.000Z" title="发表于 2025-08-12 20:43:33">2025-08-12</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-docker/" title="docker">docker</a><time datetime="2025-08-05T11:02:36.000Z" title="发表于 2025-08-05 19:02:36">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E8%BF%90%E5%8A%A8%E5%AF%B9%E8%B1%A1%E5%88%86%E5%89%B2%E4%B8%8E%E9%85%8D%E5%87%86%E7%AE%97%E6%B3%95/" title="运动对象分割与配准算法">运动对象分割与配准算法</a><time datetime="2025-08-05T08:16:02.000Z" title="发表于 2025-08-05 16:16:02">2025-08-05</time></div></div><div class="aside-list-item no-cover"><div class="content"><a class="title" href="/2025/08/05/2025-08-05-%E6%9D%A1%E4%BB%B6%E6%AC%A7%E5%BC%8F%E8%81%9A%E7%B1%BB%E5%88%86%E5%89%B2/" title="条件欧式聚类分割">条件欧式聚类分割</a><time datetime="2025-08-05T07:56:39.000Z" title="发表于 2025-08-05 15:56:39">2025-08-05</time></div></div></div></div></div></div></main><footer id="footer"><div class="footer-other"><div class="footer-copyright"><span class="copyright">&copy;&nbsp;2025 By Fi9zero</span><span class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo 7.3.0</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly 5.4.2</a></span></div><div class="footer_custom_text">Only light can attract bugs.</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="日间和夜间模式切换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><div class="js-pjax"><script>(() => {
  const loadMathjax = () => {
    if (!window.MathJax) {
      window.MathJax = {
        tex: {
          inlineMath: [['$', '$'], ['\\(', '\\)']],
          tags: 'none',
        },
        chtml: {
          scale: 1.1
        },
        options: {
          enableMenu: true,
          renderActions: {
            findScript: [10, doc => {
              for (const node of document.querySelectorAll('script[type^="math/tex"]')) {
                const display = !!node.type.match(/; *mode=display/)
                const math = new doc.options.MathItem(node.textContent, doc.inputJax[0], display)
                const text = document.createTextNode('')
                node.parentNode.replaceChild(text, node)
                math.start = {node: text, delim: '', n: 0}
                math.end = {node: text, delim: '', n: 0}
                doc.math.push(math)
              }
            }, '']
          }
        }
      }

      const script = document.createElement('script')
      script.src = 'https://cdn.jsdelivr.net/npm/mathjax/es5/tex-mml-chtml.min.js'
      script.id = 'MathJax-script'
      script.async = true
      document.head.appendChild(script)
    } else {
      MathJax.startup.document.state(0)
      MathJax.texReset()
      MathJax.typesetPromise()
    }
  }

  btf.addGlobalFn('encrypt', loadMathjax, 'mathjax')
  window.pjax ? loadMathjax() : window.addEventListener('load', loadMathjax)
})()</script><script>(() => {
  const runMermaid = ele => {
    window.loadMermaid = true
    const theme = document.documentElement.getAttribute('data-theme') === 'dark' ? 'dark' : 'default'

    ele.forEach((item, index) => {
      const mermaidSrc = item.firstElementChild
      const mermaidThemeConfig = `%%{init:{ 'theme':'${theme}'}}%%\n`
      const mermaidID = `mermaid-${index}`
      const mermaidDefinition = mermaidThemeConfig + mermaidSrc.textContent

      const renderFn = mermaid.render(mermaidID, mermaidDefinition)
      const renderMermaid = svg => {
        mermaidSrc.insertAdjacentHTML('afterend', svg)
      }

      // mermaid v9 and v10 compatibility
      typeof renderFn === 'string' ? renderMermaid(renderFn) : renderFn.then(({ svg }) => renderMermaid(svg))
    })
  }

  const codeToMermaid = () => {
    const codeMermaidEle = document.querySelectorAll('pre > code.mermaid')
    if (codeMermaidEle.length === 0) return

    codeMermaidEle.forEach(ele => {
      const preEle = document.createElement('pre')
      preEle.className = 'mermaid-src'
      preEle.hidden = true
      preEle.textContent = ele.textContent
      const newEle = document.createElement('div')
      newEle.className = 'mermaid-wrap'
      newEle.appendChild(preEle)
      ele.parentNode.replaceWith(newEle)
    })
  }

  const loadMermaid = () => {
    if (true) codeToMermaid()
    const $mermaid = document.querySelectorAll('#article-container .mermaid-wrap')
    if ($mermaid.length === 0) return

    const runMermaidFn = () => runMermaid($mermaid)
    btf.addGlobalFn('themeChange', runMermaidFn, 'mermaid')
    window.loadMermaid ? runMermaidFn() : btf.getScript('https://cdn.jsdelivr.net/npm/mermaid/dist/mermaid.min.js').then(runMermaidFn)
  }

  btf.addGlobalFn('encrypt', loadMermaid, 'mermaid')
  window.pjax ? loadMermaid() : document.addEventListener('DOMContentLoaded', loadMermaid)
})()</script></div><script defer src="https://npm.elemecdn.com/jquery@latest/dist/jquery.min.js"></script><script defer data-pjax src="/js/pig.js"></script><script src="/js/weather.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="text-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="站内搜索..." type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js"></script></div></div></body></html>